package com.example.datachangerecord.controller;

import com.example.datachangerecord.domain.PdGoods;
import com.example.datachangerecord.record.annotation.Record;
import com.example.datachangerecord.record.annotation.SelectCol;
import com.example.datachangerecord.record.emun.ColType;
import com.example.datachangerecord.record.emun.RecordTable;
import com.example.datachangerecord.record.emun.TableCol;
import com.example.datachangerecord.service.PdGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xyj
 * @date 2023/12/27
 */
@RestController
@RequestMapping("/good")
public class GoodController {

    @Autowired
    private PdGoodsService pdGoodsService;

    @GetMapping("/list")
    public List<PdGoods> list() {
        return pdGoodsService.list();
    }

    @Record(value = RecordTable.pd_goods, concernFields = TableCol.good_desc)
    @GetMapping("/update/{id}/{str}")
    public String update(@PathVariable("id") @SelectCol("id") Long id, @PathVariable("str") String str) {

        PdGoods goods = new PdGoods();
        goods.setId(id);
        goods.setGoodDesc(str);
        pdGoodsService.updateById(goods);
        return "ok";
    }

    @Record(value = RecordTable.pd_goods)
    @GetMapping("/add/{str}")
    public String add(@SelectCol(value = "good_desc", colType = ColType.STRING) @PathVariable("str") String str) {

        PdGoods goods = new PdGoods();
        goods.setGoodDesc(str);
        pdGoodsService.save(goods);
        return "ok";
    }
}
